@isTest
private class InstrumentDeliverySyncUtilTest {

    static testMethod void testMethod1() {

        UserInformation__c uinfo = new UserInformation__c();
        uinfo.SubCompany__c = '迈克生物股份有限公司';
        uinfo.OrderProcessingAssistant__c = UserInfo.getUserId();
        uinfo.SalesAssistant__c = UserInfo.getUserId();
        insert uinfo; 

         UserInformation__c uinfo1= new UserInformation__c();
        uinfo1.SubCompany__c = '四川省迈克实业有限公司';
        uinfo1.OrderProcessingAssistant__c = UserInfo.getUserId();
        uinfo1.SalesAssistant__c = UserInfo.getUserId();
        insert uinfo1; 

        
         ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
        Brand__c brand = new Brand__c(
        	BrandShortName__c = '四川迈克'
        );
        insert brand;
       Province__c province  = new Province__c();
       province.Name = '四川';

       insert province ;

         City__c city = new City__c();
       city.Province__c = province.Id;
       insert city;


         Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        acc.AccountNumber = '123456';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');
          acc.Province__c = province.Id;
        acc.City__c = city.Id;
        insert acc;     

         Dealer__c d = new Dealer__c(

        	Only__c='Dealer__c'
        );
         d.RelatedCustomers__c = acc.Id;
         insert d ;

        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;

        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

     	Product__c p  = TestUtils.createInst();
        p.Name = '测试仪器';
        p.RecordTypeId = pRT;
        p.Category0__c  = '仪器';
        p.Category1__c ='生化';
        p.ProductName__c = '测试仪器';
        p.ProductStructure__c = ps1.Id;
        p.CRMBrand__c = brand.Id;
        p.NCCode__c = '10';
        p.InstrumentType__c = '维保仪器';
        p.ProductStructure__c = ps1.Id;
        p.NCvpzwh__c = '1';
        insert p ;



        Product__c p1  = TestUtils.createInst();
        p1.Name = '测试仪器1';
        p1.RecordTypeId = pRT;
        p1.Category0__c  = '仪器';
        p1.ProductName__c = '测试仪器1';
        p1.CRMBrand__c = brand.Id;
        p1.NCCode__c = '101';
        p1.InstrumentType__c = '维保仪器';
        p1.ProductStructure__c = ps1.Id;
        p1.NCvpzwh__c = '1';
        insert p1 ;


        Product__c p2 = TestUtils.createInst();
        p2.Name = '测试仪器2';
        p2.RecordTypeId = pRT;
        p2.Category0__c  = '仪器';
        p2.ProductName__c = '测试仪器2';
       	p2.MatchingInstrument__c = p.Id;
       	 p2.NCCode__c = '02';
        p2.InstrumentType__c = '非维保仪器';
        p2.ProductStructure__c = ps1.Id;
        p2.NCvpzwh__c = '1';

        insert p2 ;

 /*
        Product__c p3= TestUtils.createInst();
        p3.Name = '打印机';
        p3.RecordTypeId = pRT;
        p3.ProductName__c = '打印机';
         p3.NCCode__c = '03';
       	//p3.MatchingInstrument__c = p.Id;
        insert p3 ;
       

        ConfigurationListDetail__c cfg = new ConfigurationListDetail__c();
        cfg.Amount__c = 1;
        cfg.Configuration__c  = p1.Id;
        cfg.InstrumentModel__c = p.Id;
        cfg.OrderMethod__c = '仪器配送';
        cfg.IsStandard__c = '必须配备（非标配）';
        cfg.Name = p1.Name;
        insert cfg;
        */


        Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
       	con.Status__c  = '已生效';
        con.Corpcode__c = 'MKSY';
        con.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalContract', 'Contract__c');
        insert con;



        ContractProduct__c cp = new ContractProduct__c();
        cp.Contract__c = con.Id;
        cp.Product__c =p.Id  ;
        cp.Quantity__c = 5;
        cp.OriginNumber__c = 5;
        cp.SalesPrice__c = 1; 
        cp.CoopType__c = '投放';
        cp.ApplicationQuantity__c = 5;
        insert cp ;


        ContractProduct__c cp1 = new ContractProduct__c();
        cp1.Contract__c = con.Id;
        cp1.Product__c =p1.Id  ;
        cp1.Quantity__c = 5;
        cp1.OriginNumber__c = 5;
        cp1.SalesPrice__c = 0; 
        cp1.CoopType__c = '投放';
        cp1.ApplicationQuantity__c = 5;
        insert cp1 ;


        ContractProduct__c cp2 = new ContractProduct__c();
        cp2.Contract__c = con.Id;
        cp2.Product__c =p2.Id  ;
        cp2.Quantity__c = 5;
        cp2.OriginNumber__c = 5;
        cp2.SalesPrice__c = 0; 
        cp2.CoopType__c = '正常销售';
        cp2.ApplicationQuantity__c = 5;
        insert cp2 ;

        InstrumentDeliveryApplication__c  ida = new InstrumentDeliveryApplication__c();
        ida.InventoryStatus__c = '迈克生物股份有限公司';
        ida.InvoicingCustomers__c = acc.Id;
        ida.ReceivingCustomer__c = acc.Id;
        ida.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('SalesOutlet', 'InstrumentDeliveryApplication__c');
        insert ida;


        InstrumentDeliverynDetails__c idd = new InstrumentDeliverynDetails__c();
        idd.DeliveryInstrument__c = cp.Id;
        idd.Number__c = 1;
        idd.ReturnToMainTable__c = ida.Id;

        insert idd ; 


        InstrumentDeliverynDetails__c idd2= new InstrumentDeliverynDetails__c();
        idd2.DeliveryInstrument__c = cp2.Id;
        idd2.Number__c = 1;
        idd2.ReturnToMainTable__c = ida.Id;


        insert idd2 ; 

        NCOutsourcingProducts__c np = new NCOutsourcingProducts__c();
        np.InstrumentDeliveryDetails__c = idd.Id;


        np.OrderMethod__c= '仪器配送';
        np.Number__c = 1;
        np.NCProducts__c = p.Id;
        np.UnitPrice__c = 10;


        insert np;

        try{

        	InstrumentDeliverySyncUtil.SyncToNC(ida.Id ,true);
        }catch(Exception e){

        }
       

        InstrumentDeliverySyncUtil.getDetails(ida.Id);

        List<Map<String,String>> data  = new List<Map<String,String>> ();
        /*Map<String,String> d1 = new Map<String,String>();
        d1.put('subId',idd2.Id);
        d1.put('eno', 'x111');
        d1.put('outno', 'xc1');
        d1.put('signdate', '2019-12-12');
        d1.put('num', '1');
        data.add(d1);
        */

        Map<String,String> d2 = new Map<String,String>();
        d2.put('subId',np.Id);
        d2.put('eno', 'x12');
        d2.put('outno', 'xc1');
        d2.put('signdate', '2019-12-12');
        d2.put('num', '1');


        
        data.add(d2);

System.debug('InstrumentDeliverySyncUtilTest==>同步投放出库'+InstrumentDeliverySyncUtil.backWriteFromNc(ida.Id,data));
        InstrumentDeliveryApplication__c  ida1 = new InstrumentDeliveryApplication__c();
        ida1.InventoryStatus__c = '迈克生物股份有限公司';
        ida1.InvoicingCustomers__c = acc.Id;
        ida1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('AbnormalSalesOutlet', 'InstrumentDeliveryApplication__c');
        ida1.DemandWarehouse__c = '迈克仪器成品库';
        ida1.GroupAllocation__c = 'Y';
        ida1.AllocationCompany__c = '四川省迈克实业有限公司';
        ida1.Status__c = '审批中' ;
        insert ida1;


        InstrumentDeliverynDetails__c idd1 = new InstrumentDeliverynDetails__c();
        idd1.DeliveryInstrument__c = cp1.Id;
        idd1.Number__c = 1;


        idd1.ReturnToMainTable__c = ida1.Id;

        insert idd1 ; 


        InstrumentDeliverynDetails__c idd3= new InstrumentDeliverynDetails__c();
        idd3.DeliveryInstrument__c = cp2.Id;
        idd3.Number__c = 1;
        idd3.ReturnToMainTable__c = ida1.Id;

        insert idd3 ; 


        NCOutsourcingProducts__c np1 = new NCOutsourcingProducts__c();
        np1.InstrumentDeliveryDetails__c = idd1.Id;


        np1.OrderMethod__c= '仪器配送';
        np1.Number__c = 1;
        np1.NCProducts__c = p.Id;
        np1.UnitPrice__c = 10;
        insert np1;
        try{

        	InstrumentDeliverySyncUtil.SyncToNC(ida1.Id ,true);
        }catch(Exception e){
            System.debug('InstrumentDeliverySyncUtil==>'+e.getMessage()+e.getStackTraceString());
        }
         try{

             System.debug('InstrumentDeliverySyncUtil==>'+InstrumentDeliverySyncUtil.postRequireBuy(ida1.Id));
        }catch(Exception e){
            System.debug('InstrumentDeliverySyncUtil==>'+e.getMessage()+e.getStackTraceString());

        }

        System.Test.startTest();

        try{
            System.debug( NotiAssiantUtil.noteContract(con.Id));
        }catch(Exception e){
            System.debug('InstrumentDeliverySyncUtil==>'+e.getMessage()+e.getStackTraceString());
        }


        try{
            System.debug( NotiAssiantUtil.noteDelivery(ida.Id));
        }catch(Exception e){
            System.debug('InstrumentDeliverySyncUtil==>'+e.getMessage()+e.getStackTraceString());
        }
       


       try{
            System.debug( NotiAssiantUtil.noteOpp(ida.Id));

        }catch(Exception e){
            System.debug('InstrumentDeliverySyncUtil==>'+e.getMessage()+e.getStackTraceString());
        }
       





        


    }

   /* static testMethod void testMethod5(){

                  //客户1
            Account acc = TestUtils.createAccount();
            acc.Level__c = '三级甲等';
            acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('DealerAbroad', 'Account');
            insert acc;     

            //业务机会1
            Opportunity opp = TestUtils.createOpportunity();
            opp.AccountId = acc.Id;
            opp.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('OverseaDearlerOpportunity', 'Opportunity');

            insert opp;

          
            //报价2
            Quote qt2 = new Quote(Name = '海外报价',OpportunityId = opp.Id);
            insert qt2;

            Id rt = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
            //产品1    
            Product__c p1 = TestUtils.createProduct();        
            p1.Category1__c = '微生物'; 
            p1.Purpose__c = '仪器报价用';
            p1.Category0__c = '仪器';  
            p1.RecordTypeId = rt;
       
            Product__c p2 = TestUtils.createProduct();
            p2.Name = 'Test1';
             p2.Purpose__c = '仪器报价用';

            p2.Category1__c = '免疫'; 
            p2.Category0__c = '仪器';  
            p2.RecordTypeId = rt;
            insert new Product__c[] {p1, p2};
            //报价产品1
            QuoteProduct__c qp1 = new QuoteProduct__c(Name = '测试报价产品2',CooperativePrice__c= 1,Product__c = p1.Id,Quote__c = qt2.Id);
            insert qp1;
            QuoteProduct__c qp2 = new QuoteProduct__c(Name = '测试报价产品2',CooperativePrice__c= 1,Product__c = p2.Id,Quote__c = qt2.Id);
            insert qp2;

            NotiAssiantUtil.noteQoute(qt2.Id);

            // String msg = ContractWebService.postOverseaQuote( qt2.Id , true );
             // System.debug('ContractWebServiceTest==>'+msg);
            
            // AdjustPriceHandler.adjustAboardQuoteToPriceList(qt2.Id , true);
            
        }*/

}